/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/***********************************************************************
 *
 * FILENAME:
 * gtk_gpsmap.c
 *
 * PURPOSE:
 *	Provide a GPS map interface to the user.
 *
 * LAST MODIFIED:
 *
 ***********************************************************************/

#ifndef _GEOIMG_MAP_H
#define _GEOIMG_MAP_H

#include <geotiff.h>
#include <geo_tiffp.h>
#include <xtiffio.h>
#include <tiffio.h>

#include <geo_normalize.h>
#include <geovalues.h>
#include <proj_api.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "String.h"
//#include "geoRefMap.h"

// helper functions
char *DecToDDec( double dfAngle, const char * pszAxis, int nPrecision );

// GeoImage is useful for storing geo-referencing information about an image
class GeoImage 
{
public:
	GeoImage();
	~GeoImage();
	
	bool Open(String file, String gfw="");
	bool Open();
	void Close(void);

	void getTiePoints(double &x, double &y);

	bool PrintCorners(int inv_flag, int dec_flag );
	bool ReportACorner(const char * corner_name, double x, double y, int inv_flag, int dec_flag );

	double getDegPerXp() { return dx_dPp; }
	double getDegPerYp() { return dy_dPp; }
	double getMPerXp() { return dx_mPp; }  // meter per x-pixel
	double getMPerYp() { return dy_mPp; }  // meter per y-pixel

	bool geoToImage(double &x, double &y);  // geographic (lat,lon) to image (x,y) [deg,deg]->[px,px]
	bool imageToGeo(double &x, double &y);  // image (x,y) to geographic (lat,lon) [px,px]->[deg,deg]

	TIFF * getTiff() { return tiff; }
	GdkPixbuf * getPixbuf() { return pixbuf; }
	
	String getGCS() { return gcsSTR; }
	String getModel() { return modelSTR; }
	String getMouseStr(double x, double y); // give values in lon,lat [deg]

	String getFile() {return filename;}  // return filename

	bool readWorldFile();
	void printImageInfo();

	int getWidth() { return width; }
	int getHeight() { return height; }

protected:
	String filename;
	String wf_filename; // contains definition file for image, e.g. image.gfw

	// the raw image
	GdkPixbuf *pixbuf;
	int width;
	int height;

	// tie points and resoultion of image
	double img_tieX; // x-image coordinate for map tie point
	double img_tieY; // y-image coordinate for map tie point
	double m_tieX;   // map tie point value - units defined by geotiff
	double m_tieY;   // map tie point value - units defined by geotiff
	double m_resX;   // pixelscale default - units defined by geotiff
	double m_resY;   // pixelscale default - units defined by geotiff

	double dx_dPp; // calculated [deg/pixel]
	double dy_dPp;
	double dx_mPp; // calculated [m/pixel]
	double dy_mPp;

	// for tif files
   TIFF     *tiff;
   GTIF     *gtif;
	GTIFDefn  defn;

	// for images using world file
	projPJ imageProj; // image projection using PROJ.4
	projUV imageUV;   // used for conversion
		
	String gcsSTR;
	String modelSTR;

	//--------- Internal functions ------------//
	bool calcDegPerPixel();

	bool image_open();
	bool geoTIFF_open();
};

#endif // _GEOREF_MAP_H
